package com.carhome.adapter;

import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.PSource;

import org.w3c.dom.ls.LSInput;

import com.carhome.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 条件搜索listview相关adapter
 *
 */
public class SearchAdapter extends BaseAdapter {

	private List<String> list;
	private Context context;

	public SearchAdapter(Context context) {
		this.context = context;
		list = new ArrayList<String>();
	}

	public int getCount() {
		return list.size();
	}

	/**
	 * 通过资源ID加载数据到adapter
	 */
	public void addDataFromArrayId(int id) {
		String[] arrys = context.getResources().getStringArray(id);
		for (int i = 0; i < arrys.length; i++) {
			list.add(arrys[i]);
		}
	}

	/**
	 * 移除集合中所有的元素
	 */
	public void removeAllData() {
		if (list == null) {
			return;
		}
		list.clear();
	}

	public void addData(List<String> list) {
		this.list = list;
	}

	public String getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.search_listview_item, null);
			holder.item = (TextView) convertView.findViewById(R.id.search_listview_item);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		holder.item.setText(getItem(position));
		return convertView;
	}

	private class ViewHolder {
		TextView item;
	}

}
